package kodkod.analysis.AST;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.text.NumberFormatter;

import kodkod.ast.Formula;

public class wEdge implements Comparable<wEdge> {
	public wNode node1;
	public wNode node2;
	String name; 
	
	double weight = 0;
	
	public String color = "black";
	public String headlabel = "";
	public String taillabel = "";
		
	protected wEdge(wNode node1, wNode node2) {
		this.node1 = node1;
		this.node2 = node2;
		this.name = node1.getId() + "->" + node2.getId();
		
		node1.addOutgoingEdge(this);
		node2.addIncomingEdge(this);
	}
	
	@Override	
	public int compareTo(wEdge w) {
		return hashCode() - w.hashCode();
	}
	
	// symmetric hashcode
	@Override
	public int hashCode() {
		if (node1.hashCode() < node2.hashCode()) {
			return node2.hashCode() * 100000 + node1.hashCode();
		} else {
			return node1.hashCode() * 100000 + node2.hashCode();
		}
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof wEdge)) {
			return false;
		}		
		return node1.equals(((wEdge)o).node1) && node2.equals(((wEdge)o).node2) || node1.equals(((wEdge)o).node2) && node2.equals(((wEdge)o).node1);
	}
	
	@Override
	public String toString() {
		if (weight > 0) {
			NumberFormat format = new DecimalFormat("#0.00");
			String label = format.format(weight);
			if (weight > 2) {
				color = "red";
			}
			if (weight < 1) {
				color = "gray";
			}
			format = new DecimalFormat("#0.00000");
			String sWeight = format.format(weight*100);
			return node1.getIdString() + "->" + node2.getIdString() + "[weight="+sWeight+" label=\""+label+"\" color=" + color + ", headlabel=\"" + headlabel + "\", taillabel=\"" + taillabel + "\"]";
		} else {
			return node1.getIdString() + "->" + node2.getIdString() + "[color=" + color + ", headlabel=\"" + headlabel + "\", taillabel=\"" + taillabel + "\"]";
		}
	}
}